package com.javasea.netty.har.common.utils;

import com.javasea.netty.har.common.entity.Heart;
import com.javasea.netty.har.common.entity.NioClient;
import com.javasea.netty.har.common.entity.Result;
import com.javasea.netty.har.common.entity.print.Command;
import com.javasea.netty.har.common.entity.print.PrintStateMsgConfirm;
import com.javasea.netty.har.common.entity.print.PrintStateMsgNotice;
import com.javasea.netty.har.common.entity.proto.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 *@ClassName Sender
 *@Description 发送类，统一进行管理，同时在发送前进行protobuf转换
 *@Author longxiaonan@163.com
 *@Date 2021/6/6 0006 11:59
 */
@Slf4j
public class Sender {

    public static void send(ChannelHandlerContext ctx, PrintStateMsgNotice printStateMsgNotice) {
        if(Objects.nonNull(ctx) && Objects.nonNull(printStateMsgNotice)) {
            PrintStateMsgProto.PrintStateMsgNotice printStateMsgNoticeProto = Converter.pojo2Protobuf(printStateMsgNotice);
            ctx.writeAndFlush(printStateMsgNoticeProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", ctx, printStateMsgNotice);
        }
//        ctx.writeAndFlush(printStateMsgNotice);
    }

    public static void send(ChannelHandlerContext ctx, Result result) {
        if(Objects.nonNull(ctx) && Objects.nonNull(result)) {
            ResultProto.Result resultProto = Converter.pojo2Protobuf(result);
            ctx.writeAndFlush(resultProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", ctx, result);
        }
//        ctx.writeAndFlush(result);
    }

    public static void send(ChannelHandlerContext ctx, NioClient client) {
        if(Objects.nonNull(ctx) && Objects.nonNull(client)) {
            NioClientProto.NioClient resultProto = Converter.pojo2Protobuf(client);
            ctx.writeAndFlush(resultProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", ctx, client);
        }
//        ctx.writeAndFlush(client);
    }

    public static void send(ChannelHandlerContext ctx, Heart heart) {
        if(Objects.nonNull(ctx) && Objects.nonNull(heart)) {
            HeartProto.Heart heartProto = Converter.pojo2Protobuf(heart);
            ctx.writeAndFlush(heartProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", ctx, heart);
        }
        // ctx.writeAndFlush(heart);
    }

    /** 客户端打印信息汇报后进行确认 */
    public static void send(ChannelHandlerContext ctx, PrintStateMsgConfirm msgConfirm) {
        if(Objects.nonNull(ctx) && Objects.nonNull(msgConfirm)){
            PrintStateMsgProto.PrintStateMsgConfirm PrintStateMsgConfirmProto = Converter.pojo2Protobuf(msgConfirm);
            ctx.writeAndFlush(PrintStateMsgConfirmProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", ctx, msgConfirm);
        }
//        ctx.writeAndFlush(msgConfirm);
    }

    public static void send(Channel channel, Command cmdparam) {
        if(Objects.nonNull(channel) && Objects.nonNull(cmdparam)){
            CommandProto.Command commandProto = Converter.pojo2Protobuf(cmdparam);
            channel.writeAndFlush(commandProto);
        }else{
            log.error("ctx或者printStateMsgNotice为空: [{}], [{}]", channel, cmdparam);
        }
//        channel.writeAndFlush(cmdparam);
    }

}
